import 'package:floor/floor.dart';
import 'package:share_fit/app/dao/dao_mixin.dart';
import 'package:share_fit/app/models/home/workout_model.dart';

@dao
abstract class WorkoutDao with DaoMixin<WorkoutModel> {
  @override
  @Query('SELECT * FROM workout WHERE _id = :id')
  Stream<WorkoutModel?> findById(int id);

  @override
  @Query('SELECT * FROM workout')
  Future<List<WorkoutModel>> findAll();

  @Query('SELECT * FROM workout where address = :address and calendar = :calendar order by start_time desc limit 1')
  Future<List<WorkoutModel>> findNewestOne(String address, String calendar);

  @Query('SELECT * FROM workout where address = :address and sports_type in (:types)')
  Future<List<WorkoutModel>> findByType(String address, List<int> types);

  @Query('SELECT * FROM workout where address = :address and sports_type in (:types) and calendar >= :start and calendar <= :end order by start_time desc')
  Future<List<WorkoutModel>> findInRangeByType(String address, List<int> types, String start, String end);

  @override
  @Query('SELECT * FROM workout')
  Stream<List<WorkoutModel>> findAllAsStream();

  @override
  @Query('delete FROM workout')
  Future<void> deleteAll();

  /// 查找指定月份中有运动数据的日期
  @Query("select distinct calendar from workout where address = :address and calendar >= :minYear and calendar <= :maxYear")
  Future<List<String>> findHasRecordDays(String address, String minYear, String maxYear);

  @Query('delete FROM workout where address = :address')
  @override
  Future<void> deleteAllByAddress(String address);
}